//
//  AlbumPersistence.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/23.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "AlbumPersistence.h"

@implementation AlbumPersistence

static AlbumPersistence *sharedManager = nil;
+ (instancetype)sharedManager
{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sharedManager = [AlbumPersistence new];
    });
    
    return sharedManager;
}

#pragma mark - 公有方法

/** 创建albumId对应的文件目录，包括子目录Thumbnail、Photo、Video */
- (void)createDirectoryAtAlbumID:(NSString *)albumID
{
    if ([self isExistDirectoryAtAlbumID:albumID]) return;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createDirectoryAtPath:[self applicationDocumentsDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self applicationDocumentsThumbnailDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self applicationDocumentsAspectRatioThumbnailDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self applicationDocumentsPhotoDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
    [fileManager createDirectoryAtPath:[self applicationDocumentsVideoDirectoryAtAlbumID:albumID] withIntermediateDirectories:NO attributes:nil error:nil];
}

/** 创建albumId对应的Thumbnail文件 */
- (void)createThumbnailAtAlbumID:(NSString *)albumID withImageData:(NSData *)imageData withImageName:(NSString *)imageName
{
    if (![self isExistThumbnailDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self applicationDocumentsThumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    
    [imageData writeToFile:path atomically:YES];
}

/** 创建albumId对应的AspectRatioThumbnail文件 */
- (void)createAspectRatioThumbnailAtAlbumID:(NSString *)albumID withImageData:(NSData *)imageData withImageName:(NSString *)imageName
{
    if (![self isExistAspectRatioThumbnailDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self applicationDocumentsAspectRatioThumbnailDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]];
    
    [imageData writeToFile:path atomically:YES];
}

/** 创建albumId对应的Photo文件 */
- (void)createPhotoAtAlbumID:(NSString *)albumID withImageData:(NSData *)imageData withImageName:(NSString *)imageName
{
    if (![self isExistPhotoDirectoryAtAlbumID:albumID]) return;
    
    NSString *directoryPath = [self applicationDocumentsPhotoDirectoryAtAlbumID:albumID];
    NSString *path = [directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path]) {
        [fileManager createFileAtPath:path contents:nil attributes:nil];
    }
    
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    if (!fileHandle) return;
    
    [fileHandle seekToEndOfFile];
    [fileHandle writeData:imageData];
    
    [fileHandle closeFile];
}

/** 创建albumId对应的Video文件 */
- (void)createVideoAtAlbumID:(NSString *)albumID withCreateDate:(NSString *)createDate
{
    if (![self isExistVideoDirectoryAtAlbumID:albumID]) return;
}

/** 返回albumId对应的Thumbnail目录 */
- (NSString *)applicationDocumentsThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Thumbnail"];

    return path;
}

/** 返回albumId对应的AspectRatioThumbnail目录 */
- (NSString *)applicationDocumentsAspectRatioThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"AspectRatioThumbnail"];
    
    return path;
}

/** 返回albumId对应的Photo目录 */
- (NSString *)applicationDocumentsPhotoDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Photo"];
    
    return path;
}

/** 返回albumId对应的Video目录 */
- (NSString *)applicationDocumentsVideoDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Video"];
    
    return path;
}

#pragma mark - 私有方法

/** 返回albumId对应的文件目录 */
- (NSString *)applicationDocumentsDirectoryAtAlbumID:(NSString *)albumID
{
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:albumID];
    
    return path;
}

/** 判断是否存在albumId对应的文件目录 */
- (BOOL)isExistDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的Thumbnail目录 */
- (BOOL)isExistThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsThumbnailDirectoryAtAlbumID:albumID];

    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的AspectRatioThumbnail目录 */
- (BOOL)isExistAspectRatioThumbnailDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsAspectRatioThumbnailDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的Photo目录 */
- (BOOL)isExistPhotoDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsPhotoDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

/** 判断是否存在albumId对应的Video目录 */
- (BOOL)isExistVideoDirectoryAtAlbumID:(NSString *)albumID
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [self applicationDocumentsVideoDirectoryAtAlbumID:albumID];
    
    BOOL isDirectory = false;
    [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
    
    return isDirectory;
}

@end
